package A一周刷爆LeetCode.A基础班.I暴力递归;

/**
 * @author wei.zhao
 * @description: 数字转字母
 * @date 2022年04月15日 15:22
 */
public class Code05_ConvertToLetterString {

    public static int number(String str) {
        if (str == null || str.length() == 0) {
            return 0;
        }
        return process(str.toCharArray(), 0);
    }

    private static int process(char[] chars, int i) {
        if (i == chars.length) {
            return 1;
        }
        if (chars[i] == '0') {
            return 0;
        }
        int result = 0;
        if (chars[i] == '1') {
            result += process(chars, i + 1);
            if (i < chars.length - 1) {
                result += process(chars, i + 2);
            }
        } else if (chars[i] == '2') {
            result += process(chars, i + 1);
            if (i < chars.length - 1 && chars[i + 1] >= '0' && chars[i + 1] <= '6') {
                result += process(chars, i + 2);
            }
        } else {
            result += process(chars, i + 1);
        }
        return result;
    }
}
